// pages/message/message.js

//获取应用实例
const app = getApp();
let api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageList: [],
    titleTop: '', //区域离顶部的高度
    scrollTop: 0, //滑动条离顶部的距离
    currentIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getRemind()

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight,
          // currentIndex: current
          currentIndex: 0
        });
      }
    });

    var token = wx.getStorageSync('TOKEN');
    that.getMessgaeList(1, token);
  },
  //  tab切换
  titleClick: function (e) {
    let that = this;
    var orderList = []

    var token = wx.getStorageSync('TOKEN');
    if (e.currentTarget.dataset.idx == 0) {
      that.getMessgaeList(1, token);
    } else if (e.currentTarget.dataset.idx == 1) {
      that.getMessgaeList(2, token);
    }
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx,
      orderList: orderList
    })
  },
  // 获取消息列表
  getMessgaeList: function (type, token) {
    var that = this;

    if (!wx.getStorageSync('TOKEN')) {
      app.goAuthpPages()
    } else {
      if (type == 1) {
        // 用户消息列表
        api._get('/msg/list', '', token).then(res => {
          if (res.code == 200) {
            var list = res.result;
            list.forEach(function (value, index) {
              var createTime = value.createTime * 1000;
              list[index]['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD H:m:s');
            });
            that.setData({
              messageList: list
            });
          } else if (res.code > 400) {
            wx.navigateTo({
              url: '../login/login',
            })
          } else {
            wx.showToast({
              title: '网络忙请重试',
              icon: 'none',
            })
          }
        })
      } else if (type == 2) {
        // 系统消息列表
        api._get('/msg/sys/list', '', token).then(res => {
          if (res.code == 200) {
            var list = res.result;
            list.forEach(function (value, index) {
              var createTime = value.createTime * 1000;
              list[index]['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD H:m:s');
            });
            that.setData({
              messageList: list
            });
          }
        })
      }
    }
  },
  detail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.messageid
    wx.navigateTo({
      url: 'detail/index?id=' + id + '&type=' + that.data.currentIndex,
    })
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    if (res.from === 'button') {
      this.setData({
        showShareModelStatus: false
      })
    } else if (res.from === 'menu') {
      console.log(res.from);
      console.log('右上角菜单转发按钮');
    }
  }
})